package org.mzyyn.qr.context;


import org.mzyyn.qr.constants.UserConstant;
import org.mzyyn.qr.vo.user.UserInfoRes;
import org.qr.xiaocao.common.enums.CommonResultEnum;
import org.qr.xiaocao.exception.ServiceException;


/**
 * 用户权限相关信息
 */
public class UserAuthContext {
    /**
     * 用户登陆相关信息存放到ThreadLocal中，方便整个请求过程取用
     */
    private static ThreadLocal<UserInfoRes> userLoginInfo = new ThreadLocal<>();
    public static void setUserLoginInfo(UserInfoRes loginInfo) {
        userLoginInfo.set(loginInfo);
    }

    /**
     * 获取用户登陆相关信息
     */
    public static UserInfoRes getUserInfo() {
        UserInfoRes userInfoRes = userLoginInfo.get();
        if( null == userInfoRes ){
            throw new ServiceException(CommonResultEnum.AUTH_ERROR);
        }
        return userInfoRes;
    }

    /**
     * 清空用户登陆相关信息
     */
    public static void clear(){
        userLoginInfo.remove();
    }

    public static Boolean isAdmin(){
        return "C7C2D441F240A29D414CC0505BC678FD".equals(getUserInfo().getQqOpenid())
                || "5E22BF7CC9FDCB88CEA04FB50F0D7A37".equals(getUserInfo().getQqOpenid());
    }

    /**
     * 审核版本检查
     * @return
     */
    public static Boolean shenheVersionCheck(){
        return UserConstant.V_SHENHE_CODE.equals(getUserInfo().getVersion());
    }
}
